package cn.b.sky.mes.mm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.mm.model.StorageLocation;
import cn.b.sky.mes.mm.model.Warehouse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.mm.model.StorageLocationType;
import cn.b.sky.mes.mm.model.condition.StorageLocationCondition;
import cn.b.sky.mes.mm.service.StorageLocationService;
import cn.b.sky.mes.mm.service.StorageLocationTypeService;
import cn.b.sky.mes.mm.service.WarehouseService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

@Controller
@RequestMapping(value = "/storageLocation/*")
public class StorageLocationController extends SecurityController {
  @Resource
  StorageLocationTypeService storageLocationTypeService;

  @Resource
  StorageLocationService storageLocationService;

  @Resource
  WarehouseService warehouseService;

  @RequestMapping(value = "storageLocationList")
  public String storageLocationList(Model model,
      @ModelAttribute("condition") StorageLocationCondition storageLocationCondition, String navTabId) {
    storageLocationCondition.setSiteCode(getSiteCode());
    if (StringUtils.isEmpty(storageLocationCondition.getOrderField())) {
      storageLocationCondition.setOrderField("createDateTime");
    }
    List<StorageLocation> storageLocations = storageLocationService
        .selectStorageLocationByCondition(storageLocationCondition);
    int count = storageLocationService.selectStorageLocationCount(storageLocationCondition);
    storageLocationCondition.setData(storageLocations);
    storageLocationCondition.setTotalCount(count);
    List<StorageLocationType> storageLocationTypes = storageLocationTypeService
        .selectUnLockStorageLocationTypes(getSiteCode());
    model.addAttribute("storageLocationTypes", storageLocationTypes);
    List<Warehouse> warehouses = storageLocationService.selectWarehouseForStorageLocation(getSiteCode());
    model.addAttribute("warehouses", warehouses);
    model.addAttribute("page", storageLocationCondition);
    model.addAttribute("navTabId", navTabId);
    return "mm/storageLocation/storageLocation_list";
  }

  @RequestMapping(value = "addStorageLocation")
  public String addStorageLocation(String navTabId, Model model) {
    List<StorageLocationType> storageLocationTypes = storageLocationTypeService
        .selectUnLockStorageLocationTypes(getSiteCode());
    model.addAttribute("storageLocationTypes", storageLocationTypes);
    List<Warehouse> warehouses = storageLocationService.selectWarehouseForStorageLocation(getSiteCode());
    model.addAttribute("warehouses", warehouses);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("pattern", "add");
    return "mm/storageLocation/storageLocation_add";
  }

  @RequestMapping(value = "modifyStorageLocation")
  public String modifyStorageLocation(String navTabId, String id, Model model) {
    List<StorageLocationType> storageLocationTypes = storageLocationTypeService
        .selectUnLockStorageLocationTypes(getSiteCode());
    model.addAttribute("storageLocationTypes", storageLocationTypes);
    List<Warehouse> warehouses = storageLocationService.selectWarehouseForStorageLocation(getSiteCode());
    model.addAttribute("warehouses", warehouses);
    StorageLocation storageLocation = storageLocationService.selectStorageLocation(getSiteCode(), id);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("storageLocation", storageLocation);
    model.addAttribute("pattern", "modify");
    return "mm/storageLocation/storageLocation_add";
  }

  @RequestMapping(value = "viewStorageLocation")
  public String viewStorageLocation(String navTabId, String id, Model model) {
    List<StorageLocationType> storageLocationTypes = storageLocationTypeService
        .selectUnLockStorageLocationTypes(getSiteCode());
    model.addAttribute("storageLocationTypes", storageLocationTypes);
    List<Warehouse> warehouses = storageLocationService.selectWarehouseForStorageLocation(getSiteCode());
    model.addAttribute("warehouses", warehouses);
    StorageLocation storageLocation = storageLocationService.selectStorageLocation(getSiteCode(), id);
    model.addAttribute("navTabId", navTabId);
    model.addAttribute("storageLocation", storageLocation);
    model.addAttribute("pattern", "read");
    return "mm/storageLocation/storageLocation_add";
  }

  @RequestMapping(value = "validateStorageLocationCode")
  @ResponseBody
  public boolean validateStorageLocationCode(String storageLocationCode, Model model) {
    if (!StringUtils.isBlank(storageLocationCode)) {
      StorageLocation storageLocation = storageLocationService
          .selectStorageLocation(getSiteCode(), storageLocationCode);
      return storageLocation == null;
    }
    return true;
  }

  @RequestMapping(value = "saveStorageLocation")
  @ResponseBody
  public DWZResult saveStorageLocation(Model model, String navTabId, String id, StorageLocation storageLocation,
      HttpServletRequest request) {
    if (storageLocation == null) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    setCreateInfo(storageLocation);
    storageLocation.setSiteCode(getSiteCode());
    if (StringUtils.isBlank(id)) {
      storageLocationService.insertStorageLocation(storageLocation);
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);
    } else {
      storageLocationService.updateStorageLocation(storageLocation);
      return dialogAjaxDone(getImeViewMessageI18n(null, "successfulOperation.M"), null, null, null, null);
    }

  }

  @RequestMapping("deleteStorageLocation")
  @ResponseBody
  public DWZResult deleteStorageLocation(Model model, String navTabId, String id, HttpServletRequest request) {
    String ids = request.getParameter("ids");
    if (StringUtils.isEmpty(ids)) {
      return ajaxError(getImeViewMessageI18n(null, "parameterNull.M"));
    }
    String[] ress = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>(ress.length);
    boolean hasFail = false;
    for (String res : ress) {
      BatchJobReport report = storageLocationService.deleteStorageLocation(getSiteCode(), res);
      reports.add(report);
      if (!report.isSuccess()) {
        hasFail = true;
      }
    }
    if (!hasFail) {
      // 直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null, getImeViewMessageI18n(null, "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone(request, reports);
    }

  }

  @RequestMapping(value = "validateStorageLocation")
  @ResponseBody
  public boolean validateStorageLocation(String storageLocationCode) {
    return null != storageLocationService.selectStorageLocation(getSiteCode(), storageLocationCode);
  }

  /**
   * 
   * <strong>Title : getStorageLocation<br></strong>
   * <strong>Description : </strong>查詢倉庫下面的庫位<br> 
   * <strong>Create on : 2016-3-4 下午4:57:32<br></strong>
   * <p>
   * @param warehousCode
   * @param model
   * @return
   * DWZResult
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人    |    修改日期    |    修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("getStorageLocation")
  @ResponseBody
  public DWZResult getStorageLocation(String warehouseCode, Model model) {
    if (StringUtils.isBlank(warehouseCode)) {
      return ajaxDone(null);
    }
    Warehouse warehouse = warehouseService.getWarehouse(warehouseCode, this.getSiteCode());
    if (warehouse == null || warehouse.getLocationManageFlag() == 0) {
      return ajaxDone(null);//没有开启库位管理
    }
    List<StorageLocation> locations = storageLocationService.selectStorageLocationByWarehouse(getSiteCode(),
        warehouseCode);
    return ajaxDone(locations);
  }

  @RequestMapping("storageLocationLookUp")
  public String storageLocationLookUp(Model model, String lookupType,
      @ModelAttribute("condition") StorageLocationCondition condition, String type) {
    condition.setSiteCode(getSiteCode());
    if (!"all".equals(type)) {
      condition.setLockFlag(0);
    }
    List<StorageLocation> list = storageLocationService.selectStorageLocationByCondition(condition);
    int count = storageLocationService.selectStorageLocationCount(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    return "mm/storageLocation/storageLocation_lookup";
  }

  @RequestMapping("storageLocationSuggest")
  @ResponseBody
  public List<StorageLocation> storageLocationSuggest(@ModelAttribute("condition") StorageLocationCondition condition,
      String type) {
    condition.setSiteCode(getSiteCode());
    if (!"all".equals(type)) {
      condition.setLockFlag(0);
    }
    return storageLocationService.selectStorageLocationSuggests(condition);
  }

}
